package com.zjdiepu.www.biz.other;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.other.OtherExpressMapper;
import com.zjdiepu.www.ibiz.other.IOtherExpressService;
import com.zjdiepu.www.model.other.dto.OtherExpress;
import com.zjdiepu.www.model.other.ext.OtherExpressExt;

/**
 * 逾期_快递状态Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月21日 14时14分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherExpressServiceImpl extends BaseService implements IOtherExpressService {
	public static final Logger logger = LoggerFactory.getLogger(OtherExpressServiceImpl.class);

	@Autowired
	private OtherExpressMapper otherExpressMapper;
	
	@Override
	public void saveUpdateAndDelete(OtherExpress otherExpress, OperateType operType) {
		switch (operType) {
		case add:
			otherExpress.initInsertData(otherExpress.getModifier(), otherExpress.getModifierName());
			otherExpressMapper.insert(otherExpress);
			break;
		case edit:
			otherExpress.initUpdateData(otherExpress.getModifier(), otherExpress.getModifierName());
			// 影响的记录数
			int updateCount = otherExpressMapper.updateByPrimaryKeySelective(otherExpress);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherExpress.initDeleteData(otherExpress.getModifier(), otherExpress.getModifierName());
			// 影响的记录数
			int deleteCount = otherExpressMapper.updateByPrimaryKeySelective(otherExpress);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherExpress> selectByExt(OtherExpressExt otherExpressExt) {
		return otherExpressMapper.selectByExt(otherExpressExt);
	}
	
	@Override
	public int countByExt(OtherExpressExt otherExpressExt){
		return otherExpressMapper.countByExt(otherExpressExt);
	}
	
	@Override
	public OtherExpress selectById(String id) {
		return otherExpressMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherExpressMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(OtherExpress otherExpress) {
		return otherExpressMapper.countRefined(otherExpress);
	}

	@Override
	public OtherExpress selectRefined(OtherExpress otherExpress) {
		return otherExpressMapper.selectRefined(otherExpress);
	}

	@Override
	public boolean repeat(OtherExpress otherExpress) {
		int count = countRefined(otherExpress);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherExpress.getId())) {
			return true;
		}
		OtherExpress dbItem = selectRefined(otherExpress);
		if(null == dbItem) {
			return false;
		}
		return !otherExpress.getId().equals(dbItem.getId());
	}

	@Override
	public void deleteByLetterLawyersId(String letterLawyersId) {
		otherExpressMapper.deleteByLetterLawyersId(letterLawyersId);
	}

}
